library(fixest)
library(kableExtra)
library(magrittr)
library(tidyverse)
library(texreg)
library(ggiplot)

################################################################################
# Data Preparation
################################################################################

# read data
df <- read_csv("../data/regdata_plus2005.csv") %>% filter(Land == 7, year != 2009)

# Regression in function form
myreg <- function(y,
                  x,
                  weights = ~Gueltige,
                  subset) {
    x <- paste(x, collapse = "+")
    ff <- paste(y, "~", x, "|Verbandsgemeindeschluessel + year^Kreisschluessel")
    feols(
        as.formula(ff),
        cluster = ~Verbandsgemeindeschluessel,
        data = df,
        weights = weights,
        subset = subset
    )
}

myregtable <- function(y,
                       x_vec = xx_did,
                       weights = ~Gueltige,
                       subset,
                       suffix = "") {
    if ((!is.null(weights)) && (weights == ~Gueltige) && (y == "Wahlbeteiligung")) {
        weights <- ~Wahlberechtigte
    }
    did <- map(
        x_vec,
        myreg,
        y = y,
        weights = weights,
        subset = subset
    )
    esttex(
        did,
        file = paste0("../results/tables/regresults_", y, suffix, ".tex"),
        replace = TRUE,
        digits = 3,
        drop = "Total number of"
    )
}

setFixest_dict(
    c(
        CDU_share = "Percentage of votes for CDU",
        SPD_share = "Percentage of votes for SPD",
        GRUENE_share = "Percentage of votes for Green Party",
        FDP_share = "Percentage of votes for FDP",
        DIE_LINKE_share = "Percentage of votes for Left Party",
        AfD_share = "Percentage of votes for AfD",
        Wahlbeteiligung = "Turnout",
        ideology = "Environmental ideology",
        to_flooded_p = "Distance to any flooding",
        flooded_area_p = "Share of area flooded",
        ratio_damaged_p = "Share of buildings damaged",
        ratio_total_p = "Share of buildings affected",
        to_flooded_0_10km_p = "0 to 10km from any flooding",
        to_flooded_10_20km_p = "10 to 20km from any flooding",
        log_to_flooded_p = "Log distance to any flooding",
        Verbandsgemeindeschluessel = "Municipality",
        Kreisschluessel = "County",
        year = "Year",
        ratio_total_10_norm_p = "Norm. Share of buildings affected 0-10km",
        ratio_total_20_norm_p = "Norm. Share of buildings affected 10-20km",
        ratio_damaged_10_norm_p = "Norm. Share of buildings damaged 0-10km",
        ratio_damaged_20_norm_p = "Norm. Share of buildings damaged 10-20km"
    )
)
setFixest_etable(
    fitstat = c("my", "n", "ar2", "wr2"),
    style.tex = style.tex("aer"),
    digits.stats = 4
)


################################################################################
# Table 1
################################################################################
xx <- list(
    "log_to_flooded_p",
    "to_flooded_0_10km_p",
    c(
        "to_flooded_0_10km_p",
        "to_flooded_10_20km_p"
    ),
    "flooded_area_p",
    "ratio_total_p",
    "ratio_damaged_p"
)
myregtable("GRUENE_share", x_vec = xx, suffix = "_plus_2005")
